package com.yonyou.web.views;

import android.app.Activity;
import android.content.Context;
import android.net.http.SslError;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.yonyou.web.R;
import com.yonyou.web.interactive.NativeJavaScriptInterface;

/**
 * Created by lxp on 2017/7/27.
 */

public class ProgressBarWebView extends BaseLinearLayout {

    private ProgressBar mProgressBar;
    private WebView mWebView;
    private WebSettings mWebSettings;
    private boolean isUseProgressBar = true;

    public ProgressBarWebView(Context context) {
        super(context);
    }

    public ProgressBarWebView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressBarWebView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ProgressBarWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public int getLayoutId() {
        return R.layout.view_webview;
    }


    @Override
    public void initViews() {
        mRootView = LayoutInflater.from(mContext).inflate(R.layout.view_webview, this);
        mProgressBar = (ProgressBar) mRootView.findViewById(R.id.pb);
        mWebView = (WebView) mRootView.findViewById(R.id.webView);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(android.webkit.WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//不加上白边
        mWebView.setWebChromeClient(new MyWebChromeClient((Activity) mContext) {
            @Override
            public void onProgressChanged(android.webkit.WebView view, int newProgress) {
                if (isUseProgressBar) {
                    mProgressBar.setProgress(newProgress);
                    if (mProgressBar != null && newProgress != 100) {
                        mProgressBar.setVisibility(View.VISIBLE);
                    } else if (mProgressBar != null) {
                        mProgressBar.setVisibility(View.GONE);
                    }
                }

                super.onProgressChanged(view, newProgress);
            }
        });

        addJavaScript(new NativeJavaScriptInterface(), "NativeJavaScriptInterface");
    }

    @Override
    protected void initDatas() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置是否显示进度条
     *
     * @param useProgressBar
     */
    public void setUseProgressBar(boolean useProgressBar) {
        isUseProgressBar = useProgressBar;
    }


    /**
     * 加载网页
     *
     * @param url
     */
    public void loadUrl(String url) {
        if (null != mWebView) {
            mWebView.loadUrl(url);
        }
    }


    /**
     * 返回
     */
    public boolean back() {
        if (null != mWebView && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 增加JavaScript
     *
     * @param object 类
     * @param jsName 调用前缀
     */
    public void addJavaScript(Object object, String jsName) {
        if (null != mWebView && null != object && !TextUtils.isEmpty(jsName)) {
            mWebView.addJavascriptInterface(new NativeJavaScriptInterface(), jsName);
        } else {
            Log.e("ProgressBarWebView","注入JS有误");
        }
    }

}
